package jys.test.yddata;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import java.io.IOException;

import jys.test.yddata.ui.ToolbarPlus;
import jys.test.yddata.utils.PropertiesUtil;
import jys.test.yddata.utils.DescriptionManager;

public class DocumentActivity extends AppCompatActivity {
    private DescriptionManager descriptionManager;

    // 加载 web 界面
    public String hd = "<meta charset=\"utf-8\" emacsmode=\"-*- markdown -*-\">\n" +
            "<link rel=\"stylesheet\" href=\"https://casual-effects.com/markdeep/latest/apidoc.css\">\n" +
            "<!-- Markdeep Head -->\n\n";
    public String ft = "\n\n<!-- Markdeep Foot -->\n" +
            "<style class=\"fallback\">body{visibility:hidden}</style>\n" +
            "<script>markdeepOptions={tocStyle:'medium'};</script>\n" +
            "<script src=\"https://morgan3d.github.io/markdeep/latest/markdeep.min.js\" charset=\"utf-8\"></script>";

    public String message = "**示例文档**\n" +
            "\n" +
            "数据集描述和各类链接，如[搜狗](https://www.sogou.com/)。\n" +
            "\n" +
            "## 二级标题\n" +
            "\n" +
            "* 这个是\n" +
            "* 无序列表\n" +
            "\n" +
            "或者：\n" +
            "\n" +
            "1. 可以是\n" +
            "1. 有序列表\n" +
            "\n" +
            "还可以有表格：\n" +
            "\n" +
            "标签类型  | 描述\n" +
            "---------|-------\n" +
            "猫       | 可爱\n" +
            "狗       | 可靠\n" +
            "\n" +
            "\n" +
            "## 各类格式\n" +
            "\n" +
            "比如： **加粗**, _斜体_, ~~删除线~~\n" +
            "\n" +
            "小代码栏：`print 'hello world!'`\n" +
            "\n" +
            "大代码栏：\n" +
            "```python\n" +
            "import os\n" +
            "os.removedirs(\"/test\")\n" +
            "```\n" +
            "\n" +
            "箭头也行： x -> y ； 1 ==> 2 <==> 3.\n" +
            "\n" +
            "可以是标签\n" +
            ":   解释\n" +
            "Code\n" +
            ":   是代码的意思\n" +
            "\n" +
            "\n" +
            "## ASCII ART 图\n" +
            "\n" +
            "图通常被封闭在一个由“*”符号组成的矩形中；并使用ASCII艺术风格“绘制”，带有“-|+/\\*o”等。\n" +
            "\n" +
            "*******************************************  这是图表右边的一段文字，\n" +
            "* +-----------------+           .-.       *  很一般。\n" +
            "* |\\                |        .-+   |      *  \n" +
            "* | \\     A-B   *---+--> .--+       '--.  *  \n" +
            "* |  \\              |   |     Cloud!    | *  \n" +
            "* +---+-------------+    '-------------'  *  \n" +
            "*******************************************  \n" +
            "\n" +
            "\n" +
            "### 我得说，真是太酷了！\n" +
            "\n" +
            "********************\n" +
            "*    +-+-+-+-*-o   *\n" +
            "*   / / ^ /        *\n" +
            "*  / v / /         *\n" +
            "* +-+-+-+          *\n" +
            "********************\n" +
            "\n" +
            "!!! Note: 我是标题\n" +
            "    嘿，我是一张纸条。\n" +
            "\n" +
            "!!! WARNING\n" +
            "    我是个警告，*可能会发生什么事*\n" +
            "\n" +
            "!!! ERROR: 也可以是错误笔记\n" +
            "    错误内容\n" +
            "\n" +
            "更多语法可以参见 [Mark Deep](https://casual-effects.com/markdeep/) ~";

    private ToolbarPlus mToolbar;
    private WebView webView;

    private PropertiesUtil projectProp, serverProp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document);

        mToolbar = findViewById(R.id.settingBar);
        webView = findViewById(R.id.webView);

        try {
            projectProp = PropertiesUtil.buildFromDisk(this, "project");
            serverProp = PropertiesUtil.buildFromDisk(this, "server");
        } catch (IOException e) {
            e.printStackTrace();
            seriousError("配置加载出错，Exp ["+e.getMessage()+"]");
        }

        try { initView(); } catch (Exception e) {
            e.printStackTrace();
            seriousError("视图加载出错，Exp ["+e.getMessage()+"]");
        }

        // 加载服务器配置及发送功能
        descriptionManager = new DescriptionManager.Builder()
                .init(this,
                        this::endLoading_Description,
                        serverProp.getProperty("description.url"))
                .build();
        descriptionManager.sendVerify(projectProp.getProperty("server.verify", "0"));
    }

    public void initView() {
        // 加载 Toolbar
        mToolbar.settingLeft(R.drawable.ic_left_arrow, (v)->finish())
                .settingTitle("数据集描述文档");
        setSupportActionBar(mToolbar);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.loadDataWithBaseURL(null, hd+"**正在加载，请稍后...**"+ft, "text/html", "UTF-8", null);
    }

    public void endLoading_Description(boolean isOK) {
        if (isOK)
            webView.loadDataWithBaseURL(null, hd+descriptionManager.description+ft, "text/html", "UTF-8", null);
        else
            webView.loadDataWithBaseURL(null, hd+"加载失败，请重试"+ft, "text/html", "UTF-8", null);
    }

    public void seriousError(String message) {
        new AlertDialog.Builder(this)
                .setTitle("错误")
                .setMessage(message)
                .setPositiveButton("退出", (dialog, which)-> finish())
                .show();
    }
}
